package cn.shen.edit.util;


import cn.hutool.core.bean.BeanUtil;
import cn.shen.dao.entity.User;
import cn.shen.dao.entity.Video;
import com.auth0.jwt.JWT;
import com.auth0.jwt.impl.JWTParser;
import io.jsonwebtoken.*;

import java.util.Date;

public class JwtUtil {

    public static final String SUCCESS_TOKEN = "SUCCESS-TOKEN";

    private  static long time = 1000*60*60*24;

    private static String signature = "/*-asd++2";

    public static String createUserToke(User user){
        JwtBuilder jwtBuilder = Jwts.builder();
        String token = jwtBuilder
                /** header*/
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
                /** payload*/
                .claim("user",user)
                .setExpiration(new Date(System.currentTimeMillis()+time)) /**有效期一天*/
                /** signature*/
                .signWith(SignatureAlgorithm.HS256,signature)
                .compact();
                System.out.println("测试git提交");
        return token;
    }

    public static User parseUserToke(String token){
        JwtParser jwtParser = Jwts.parser();
        Jws<Claims> claimsJws = jwtParser.setSigningKey(signature).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        body.get("user");
        User user = new User();
        BeanUtil.copyProperties(body.get("user"),user);
        return  user;

    }


}
